package com.qo.qsdk.utils;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import com.appsflyer.AppsFlyerConversionListener;
import com.appsflyer.AppsFlyerLib;
import com.appsflyer.attribution.AppsFlyerRequestListener;

import java.util.HashMap;
import java.util.Map;

/**
 * google pay支付
 */
public class AppsFlyerManager {

    private static final String TAG = AppsFlyerManager.class.getSimpleName();
    private Activity activity;

    private static class AppsFlyerManagerHolder {
        private static final AppsFlyerManager instance_ = new AppsFlyerManager();
    }

    public static AppsFlyerManager getInstance() {
        return AppsFlyerManagerHolder.instance_;
    }

    private boolean open() {
        return true;
    }

    public AppsFlyerManager() {

    }

    public void init(final Context activity, String key) {
        if (open()) {
            AppsFlyerConversionListener conversionListener = new AppsFlyerConversionListener() {
                @Override
                public void onConversionDataSuccess(Map<String, Object> conversionData) {
                    for (String attrName : conversionData.keySet()) {
                        Log.d(TAG, "attribute: " + attrName + " = " + conversionData.get(attrName));
                    }
                }

                @Override
                public void onConversionDataFail(String errorMessage) {
                    Log.d(TAG, "error getting conversion data: " + errorMessage);
                }

                @Override
                public void onAppOpenAttribution(Map<String, String> attributionData) {
                    for (String attrName : attributionData.keySet()) {
                        Log.d(TAG, "attribute: " + attrName + " = " + attributionData.get(attrName));
                    }
                }

                @Override
                public void onAttributionFailure(String errorMessage) {
                    Log.d(TAG, "error onAttributionFailure : " + errorMessage);
                }
            };

            if (QSDKHelper.getInstance().getConfigureModel().isDebug()) {
                AppsFlyerLib.getInstance().setDebugLog(true);
            }

            AppsFlyerLib.getInstance().init(key, conversionListener, activity);

            AppsFlyerLib.getInstance().start(activity);
        }
    }

    public void log(Context context, String event) {
        log(context, event, null);
    }

    public void log(Context context, final String event, final Map<String,Object> bundle) {
        if (open()) {
            AppsFlyerLib.getInstance().logEvent(context, event, bundle == null ? new HashMap<String, Object>() : bundle, new AppsFlyerRequestListener() {
                @Override
                public void onSuccess() {
                    Log.e(TAG, "name:" + event + ",val->" + (bundle == null ? "" : bundle.toString()) + " ===> success");
                }

                @Override
                public void onError(int i, @NonNull String s) {
                    try {
                        Log.e(TAG, "name:" + event + ",val->" + (bundle == null ? "" : bundle.toString()) + " ===> fail :" + s);
                    }catch (Exception e){}
                }
            });
        }
    }
}
